﻿using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Utility
{
    /// <summary>
    /// DocumentUtils 的摘要说明
    /// </summary>
    public class DocumentUtils
    {
        public DocumentUtils()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        #region 获取文档项全部路径

        public string GetFullPath(DocumentItem item, char seperator)
        {
            StringBuilder path = new StringBuilder(item.Path);

            DocumentItem parent = item.Parent;
            while (parent != null)
            {
                path.Insert(0, seperator);
                path.Insert(0, parent.Path);
                // 向上访问
                parent = parent.Parent;
            }

            return path.ToString();
        }

        public string GetFullPath(DocumentItem item)
        {
            return GetFullPath(item, '/');
        }

        #endregion 获取文档项全部路径

        #region 从XML文档中加载

        public DocumentItemCollection LoadFromXml(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlElement root = doc.DocumentElement;
            if (root.Name == "HelpDocument")
            {
                DocumentItem result = new DocumentItem();

                // 生成顶层
                result.Index = int.Parse(root.GetAttribute("Index"));
                result.Path = root.GetAttribute("Path");
                result.Text = root.GetAttribute("Text");

                // 遍历层节点
                DocumentItemCollection items = ParseCollection(root.ChildNodes, result);
                items.Insert(0, result);
                return items;
            }

            return null;
        }

        private DocumentItemCollection ParseCollection(XmlNodeList nodes, DocumentItem parent)
        {
            DocumentItemCollection items = new DocumentItemCollection();
            foreach (XmlNode node in nodes)
            {
                if (node.NodeType == XmlNodeType.Element && node.Name == "Item")
                {
                    DocumentItem item = new DocumentItem();

                    item.Index = int.Parse(node.Attributes["Index"].Value);
                    item.Path = node.Attributes["Path"].Value;
                    item.Text = node.Attributes["Text"].Value;
                    item.Parent = parent;

                    items.Add(item);

                    DocumentItemCollection children = ParseCollection(node.ChildNodes, item);
                    if (children.Count > 0)
                    {
                        items.AddRange(children);
                    }
                }
            }
            return items;
        }

        #endregion 从XML文档中加载

        #region 检索

        /// <summary>
        /// 获取父项的下级直属项
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public DocumentItemCollection GetChildItems(DocumentItem item, DocumentItemCollection source)
        {
            DocumentItemCollection result = new DocumentItemCollection();

            foreach (DocumentItem i in source)
            {
                if (i.Parent == item)
                {
                    result.Add(i);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取指定索引号的项
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>返回索引号存在的项，不存在返回null</returns>
        public DocumentItem GetItemByIndex(int index, DocumentItemCollection source)
        {
            foreach (DocumentItem item in source)
            {
                if (item.Index == index)
                {
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// 检查指定项是否包含于指定容器内
        /// </summary>
        /// <param name="item"></param>
        /// <param name="container"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public bool Contains(DocumentItem item, DocumentItem container)
        {
            DocumentItem temp = item.Parent;
            while (temp != null)
            {
                if (temp == container)
                {
                    return true;
                }
                temp = temp.Parent;
            }

            return false;
        }

        #endregion 检索

        #region 生成JSON字符串

        public string ParseItemString(DocumentItemCollection items)
        {
            if (items == null || items.Count == 0)
            {
                return "[]";
            }
            StringBuilder result = new StringBuilder();
            result.Append("[");
            foreach (DocumentItem item in items)
            {
                result.AppendFormat("{{text:'{0}', path:'{1}', index: {2}}},",
                    item.Text, GetFullPath(item), item.Parent == null ? -1 : item.Parent.Index);
            }

            result.Length--;
            result.Append("]");
            return result.ToString();
        }

        #endregion 生成JSON字符串
    }

    #region 帮助项模型文件

    public class DocumentItem
    {
        public DocumentItem()
        {
        }

        #region Index

        private int _index;

        /// <summary>
        /// 设置或获取项Index
        /// </summary>
        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }

        #endregion Index

        #region Text

        private string _text;

        /// <summary>
        /// 设置或获取展示文本
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        #endregion Text

        #region Path

        private string _path;

        /// <summary>
        /// 设置或获取文件路径
        /// </summary>
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        #endregion Path

        #region Parent

        private DocumentItem _parent;

        /// <summary>
        /// 设置或获取上级项
        /// </summary>
        public DocumentItem Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        #endregion Parent
    }

    public class DocumentItemCollection : List<DocumentItem>
    { }

    #endregion 帮助项模型文件
}